package com.cqc.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
public class HelloController {

    /**
     * http://localhost:8080/f1
     */
    @GetMapping("f1")
    public String f1(HttpServletRequest request) {
        return request.getRequestURL().toString();
    }

    @Autowired
    HttpServletRequest request;

    /**
     * http://localhost:8080/f2
     */
    @GetMapping("f2")
    public String f2() {
        return request.getRequestURL().toString();
    }


    /**
     * http://localhost:8080/f3
     */
    @GetMapping("f3")
    public String f3() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getRequestURL().toString();
    }
}
